<!-- 仓库编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="460"
    :model-value="modelValue"
    :title="isUpdate ? '编辑仓库' : '新增仓库'"
    @update:model-value="updateModelValue"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="form.name" clearable :maxlength="50" placeholder="请填写仓库名称" />
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input v-model="form.consignee" clearable :maxlength="50" placeholder="请填写收货人" />
      </el-form-item>
      <el-form-item label="收货电话" prop="consignee_phone">
        <el-input v-model="form.consignee_phone" clearable :maxlength="50" placeholder="请填写收货电话" />
      </el-form-item>
      <el-form-item label="收货地址" prop="consignee_address">
        <el-input v-model="form.consignee_address" type="textarea" :autosize="{ minRows: 1, maxRows: 3 }" clearable :maxlength="50" placeholder="请填写收货地址" />
      </el-form-item>
      <el-form-item label="虚拟仓" prop="type">
        <el-switch
          v-model="form.type"
          inline-prompt
          :active-value="1"
          :inactive-alue="0"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status" placeholder="请选择状态">
          <el-radio label="正常" :value="1" />
          <el-radio label="禁用" :value="2" />
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="updateModelValue(false)">
        取消
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="save"
      >
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { updateWarehouse } from '@/api/mch-archives/warehouse'

const props = defineProps({
  /** 弹窗是否打开 */
  modelValue: Boolean,
  /** 修改回显的数据 */
  data: Object,
})

const emit = defineEmits(['done', 'update:modelValue'])

/** 是否是修改 */
const isUpdate = ref(false)

/** 提交状态 */
const loading = ref(false)

/** 表单实例 */
const formRef = ref(null)

/** 表单数据 */
const [form, resetFields, assignFields] = utils.useFormData({
  id: undefined,
  name: '',
  consignee: '',
  consignee_phone: '',
  consignee_address: '',
  type: 0,
  status: 1,
})

/** 表单验证规则 */
const rules = reactive({
  name: {
    required: true,
    message: '请填写仓库名称',
    type: 'string',
    trigger: 'blur',
  },
  consignee: {
    required: true,
    message: '请填写收货人',
    type: 'string',
    trigger: 'blur',
  },
  consignee_phone: {
    required: true,
    message: '请填写收货电话',
    type: 'string',
    trigger: 'blur',
  },
  consignee_address: {
    required: true,
    message: '请填写收货地址',
    type: 'string',
    trigger: 'blur',
  },
})

/** 保存编辑 */
function save() {
  formRef.value?.validate?.((valid) => {
    if (!valid)
      return

    loading.value = true
    updateWarehouse(form)
      .then((res) => {
        utils.message(res.msg, res.success)
        updateModelValue(false)
        emit('done')
      })
      .finally(() => {
        loading.value = false
      })
  })
}

/** 更新modelValue */
function updateModelValue(value) {
  emit('update:modelValue', value)
}

watch(
  () => props.modelValue,
  (modelValue) => {
    if (modelValue) {
      if (props.data) {
        assignFields(props.data)
        isUpdate.value = true
      }
      else {
        isUpdate.value = false
      }
    }
    else {
      resetFields()
      formRef.value?.clearValidate?.()
    }
  },
)
</script>
